package com.ld.security.shop.crawler.modules.re.crawler.fulinlin;

import com.ld.security.shop.crawler.modules.re.constant.CrawlerConstant;
import com.ld.security.shop.crawler.modules.re.crawler.BaseCrawler;
import com.ld.security.shop.crawler.modules.re.entity.ReDetails;
import com.ld.security.shop.crawler.modules.re.entity.RePage;
import com.ld.security.shop.crawler.modules.re.service.IReDetailsService;
import com.ld.security.shop.crawler.modules.re.service.IRePageService;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedList;

@Component
public class FulinlinRePageCrawler extends BaseCrawler<RePage> {

    @Autowired
    IRePageService iRePageService;

    @Autowired
    IReDetailsService iReDetailsService;

    @Autowired
    FulinlinReDetailsCrawler fulinlinReDetailsCrawler;

    public FulinlinRePageCrawler() {
        super( CrawlerConstant.projectCode.fulinlin);
    }

    @Override
    public RePage dataCrawler(String url) {
        RePage page = super.dataCrawler(url);

        Document document =this.parse(page.getContent());

        Element content =  document.select("#content").first();
        page.setContent(content.html());

        Elements pages =  document.select(".pagination  a");
        for (Element element : pages) {
            Element a = element;
            String href = a.attr("href");

            Element i =  a.select("i").first();
            if(i != null){
                String text = i.attr("aria-label");
                if ("上一页".equals(text)) {
                    page.setPreUrl(href);
                } else if ("下一页".equals(text)) {
                    page.setNextUrl(href);
                }
            }

        }
        iRePageService.save(page);
        LinkedList linkedList =   new LinkedList<>();

        for (Element a : document.select(".post-body .btn")) {
            String href = a.attr("href");
            ReDetails o = fulinlinReDetailsCrawler.dataCrawler(href);
            linkedList.add(o);
        }
        iReDetailsService.saveBatch(linkedList);
        return page;
    }




}

